define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'prizes/index' + location.search,
                    add_url: 'prizes/add',
                    edit_url: 'prizes/edit',
                    del_url: 'prizes/del',
                    multi_url: 'prizes/multi',
                    import_url: 'prizes/import',
                    table: 'prizes',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'activity.name', title: __('Activity.name'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'),visible:false, operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'probability', title: __('Probability'), operate:'BETWEEN'},
                        {field: 'activity_id', title: '活动id',visible:false},
                        // {field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'score', title: __('Score'), operate:'BETWEEN'},
                        // {field: 'depot_id', title: __('Depot_id')},
                        {field: 'depot.name', title: __('Depot.name'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            //活动判断
            $('#score').hide();
            $('#depot').show();
            $('#c-score').attr('data-rule','')
            $('#c-depot_id').attr('data-rule','required')

            // #c-selectpage 根据元素选择器自行修改
            $("#c-activity_id").data("eSelect", function(e){
                //后续操作
                //1 抽奖 2 积分兑换
                if(e.id==1){
                    $('#score').hide();
                    $('#probability').show();
                    $('#c-score').removeAttr('data-rule')
                    $('#c-score').val('')
                    $('#c-probability').attr('data-rule','required')
                    $('#c-score').attr('disabled', 'disabled');
                    $('#c-probability').removeAttr('disabled');
                }else{
                    $('#probability').hide();
                    $('#score').show();
                    $('#c-score').attr('data-rule','required')
                    $('#c-probability').removeAttr('data-rule')
                    $('#c-probability').val('')
                    $('#c-probability').attr('disabled', 'disabled');
                    $('#c-score').removeAttr('disabled');
                }

            });
            Controller.api.bindevent();
        },
        edit: function () {
            let type = $("select[name='row[type]']").val();
            type = 1;
            if(type==1){
                //$('#score').hide();
                $('#depot').show();
                //$('#c-score').removeAttr('data-rule')
                //$('#c-score').val('0')
                $('#c-depot_id').attr('data-rule','required')
                //$('#c-score').attr('disabled', 'disabled');
                $('#c-depot_id').removeAttr('disabled');
            }

            //初始加载
           let type2 =  $('#c-activity_id').val()
            if(type2==1){
                $('#score').hide();
                $('#probability').show();
                $('#c-score').removeAttr('data-rule')
                $('#c-score').val('')
                $('#c-probability').attr('data-rule','required')
                $('#c-score').attr('disabled', 'disabled');
                $('#c-probability').removeAttr('disabled');
            }else{
                $('#probability').hide();
                $('#score').show();
                $('#c-score').attr('data-rule','required')
                $('#c-probability').removeAttr('data-rule')
                $('#c-probability').val('')
                $('#c-probability').attr('disabled', 'disabled');
                $('#c-score').removeAttr('disabled');
            }
            //选择加载
            $("#c-activity_id").data("eSelect", function(e){
                //后续操作
                //1 抽奖 2 积分兑换
                if(e.id==1){
                    $('#score').hide();
                    $('#probability').show();
                    $('#c-score').removeAttr('data-rule')
                    $('#c-score').val('')
                    $('#c-probability').attr('data-rule','required')
                    $('#c-score').attr('disabled', 'disabled');
                    $('#c-probability').removeAttr('disabled');
                }else{
                    $('#probability').hide();
                    $('#score').show();
                    $('#c-score').attr('data-rule','required')
                    $('#c-probability').removeAttr('data-rule')
                    $('#c-probability').val('')
                    $('#c-probability').attr('disabled', 'disabled');
                    $('#c-score').removeAttr('disabled');
                }

            });
            /*$("select[name='row[type]']").change(function (e) {
                let type = $("select[name='row[type]']").val();
                if(type==1){
                    $('#score').hide();
                    $('#depot').show();
                    $('#c-score').removeAttr('data-rule')
                    $('#c-score').val('0')
                    $('#c-depot_id').attr('data-rule','required')
                    $('#c-score').attr('disabled', 'disabled');
                    $('#c-depot_id').removeAttr('disabled');
                }else{
                    $('#depot').hide();
                    $('#score').show();
                    $('#c-score').attr('data-rule','required')
                    $('#c-depot_id').removeAttr('data-rule')
                    $('#c-depot_id').val('')
                    $('#c-depot_id').attr('disabled', 'disabled');
                    $('#c-score').removeAttr('disabled');
                }
            });*/
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});